﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.Aide.data;

namespace MeTalker.Aide.Forms
{
    internal partial class frmMenuNavigation : MeForm
    {
        List<Menu> menuList = new List<Menu>();
        MenuNavigation menus;
        public frmMenuNavigation()
        {
            InitializeComponent();
            this.dataGridView.AutoGenerateColumns = false;
            this.FormClosing += new FormClosingEventHandler(frmMenuNavigation_FormClosing);
            menus = DataFactory.Menus;
            BindData();
        }

        void frmMenuNavigation_FormClosing(object sender, FormClosingEventArgs e)
        {
            SerializerFactory.SerializeXml(menus);
            menuList.Clear();
        }

        void BindData()
        {
            menuList.Clear();
            foreach (var d in menus.Nodes)
            {
                menuList.Add(new Menu() { Id = d.Id, MenuName = d.Title, Flag = menus.DefaultId == d.Id ? "是" : "否" });
            }
            this.dataGridView.DataSource = null;
            this.dataGridView.DataSource = menuList;
            //this.dataGridView.Refresh();
        }

        private void btnBind_Click(object sender, EventArgs e)
        {
            new frmBindMenu().ShowDialog();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmMenu message = new frmMenu(null, false);
            DialogResult result = message.ShowDialog();
            if (message.Nodes != null)
            {
                int i = 1;
                while (true)
                {
                    var d = from temp in menus.Nodes where temp.Id == i select temp;
                    if (d.Count() <= 0)
                    {
                        break;
                    }
                    i++;
                }
                message.Nodes.Id = i;
                menus.Nodes.Add(message.Nodes);
                BindData();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            UtilHelper.RunBrowser(UtilHelper.HELP_BASE_URL + "62.html");
        }

        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1) //单元格当然要排除列头
            {
                // DataGridViewCell cell = this.dataGridView[0, e.RowIndex];
                Menu menu = this.dataGridView.CurrentRow.DataBoundItem as Menu;
                var d = from temp in menus.Nodes where temp.Id == menu.Id select temp;
                MessageNode node = d.FirstOrDefault();

                //也要判断当前点击单元格的名称是否是Link列的名称
                if (this.dataGridView.Columns[e.ColumnIndex].Name == "edit" && menu != null)
                {
                    if (node != null)
                    {
                        DialogResult result = new frmMenu(node, false).ShowDialog();
                        dataGridView.CurrentRow.Cells[1].Value = node.Title;
                    }
                }
                else if (this.dataGridView.Columns[e.ColumnIndex].Name == "delete" && menu != null)
                {
                    //dataGridView.Rows.Remove(dataGridView.CurrentRow);
                    //menuList.Remove(menu);
                    menus.Nodes.Remove(node);
                    //this.dataGridView.Refresh();
                    BindData();
                }
                else if (this.dataGridView.Columns[e.ColumnIndex].Name == "setting" && menu != null)
                {
                    foreach (var dd in this.dataGridView.Rows)
                    {
                        DataGridViewRow row = dd as DataGridViewRow;
                        if (row != null)
                            row.Cells[e.ColumnIndex].Value = "否";
                    }
                    menus.DefaultId = menu.Id;
                    this.dataGridView[e.ColumnIndex, e.RowIndex].Value = "是";
                }
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "*.jlso|*.jlso";
            save.FileName = "";
            save.ShowDialog();
            string fileName = save.FileName;
            if (!string.IsNullOrEmpty(fileName))
            {
                SerializerFactory.SerializeFile<MenuNavigation>(menus, fileName);
            }

        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "*.jlso|*.jlso";
            open.FileName = "";
            open.ShowDialog();
            string fileName = open.FileName;
            if (!string.IsNullOrEmpty(fileName))
            {
                menus = SerializerFactory.DeserializeFile<MenuNavigation>(fileName) as MenuNavigation;
                if (menus != null)
                    BindData();
            }
        }
    }

    public class Menu
    {
        public int Id { get; set; }
        public string MenuName { get; set; }
        public string Flag { get; set; }
    }
}